/** 
 * Description:用户评价管理
 * Date:2012-7-21
 * Author:Along	
*/
package com.community.shopping.service.logic;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.community.shopping.dao.GoodsCommentDao;
import com.community.shopping.domain.Goods;
import com.community.shopping.domain.GoodsComment;
import com.community.shopping.domain.User;
public class GoodsCommentManager {
	
	public GoodsCommentDao goodsCommentDao;

	/**
	 * @param uerDao the uerDao to set
	 */
	public void setGoodsCommentDao(GoodsCommentDao goodsCommentDao) {
		this.goodsCommentDao = goodsCommentDao;
	}
	private GoodsManager goodsManager;
	/**
	 * @param goodsManager the goodsManager to set
	 */
	public void setGoodsManager(GoodsManager goodsManager) {
		this.goodsManager = goodsManager;
	}

	private SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	//保存用户发表商品评价
	public void saveComment(User user,String commentinfo)throws Exception
	{
		String [] comments=commentinfo.split("~@#@");
		for(int i=0;i<comments.length;i++)
		{
			String[] commentstr=comments[i].split("_");
			String goodsid=commentstr[0];
			String score=commentstr[1];
			String comment=commentstr[2];
			comment=java.net.URLDecoder.decode(comment, "UTF-8"); 
			Goods goods=goodsManager.getGoodsById(Integer.parseInt(goodsid));
			GoodsComment goodscomment=new GoodsComment();
			goodscomment.setGoods(goods);
			goodscomment.setUser(user);
			String username=user.getUserName();
			username=username.substring(0, 3)+"****"+username.substring(7);
			String address="["+user.getAddress().substring(0,4)+"]";
			goodscomment.setUserName(username+address);
			goodscomment.setScore(Integer.parseInt(score));
			goodscomment.setContent(comment);
			goodscomment.setCreateTime(format.format(new Date()));
			goodscomment.setStatus(1);
			goodsCommentDao.save(goodscomment);
			updateGoodsInfo(goods);
		}
		
	}
	
	//保存用户发表商品评价
	public int saveComment(String goodsid,String user,String time,String score,String comment)throws Exception
	{
		    Goods goods=goodsManager.getGoodsById(Integer.parseInt(goodsid));
			GoodsComment goodscomment=new GoodsComment();
			goodscomment.setGoods(goods);
			goodscomment.setUser(null);
			goodscomment.setUserName(user);
			goodscomment.setScore(Integer.parseInt(score));
			goodscomment.setContent(comment);
			goodscomment.setCreateTime(time);
			goodscomment.setStatus(1);
			goodsCommentDao.save(goodscomment);
			updateGoodsInfo(goods);
			return goods.getCommentsTotal();
	}
	
	//更新商品评价分数和评论条数
	private void updateGoodsInfo(Goods goods)
	
	{
		Float avgScore=goodsCommentDao.getGoodsAvgScore(goods.getId());
		goods.setCommentScore(avgScore);
		Integer totalrows=goodsCommentDao.getTotalCount(goods.getId());
		goods.setCommentsTotal(totalrows);
		goodsManager.save(goods);
	}
	
	//查询用户评价，带翻页
	public  List queryOrderComment(String goodsid,Page page)
	{
		String sql="  goods.id="+goodsid+" and status=1 ";
		return goodsCommentDao.getGoodsComment(sql, page);
	}
		

}
